package org.swxjava.core;

import java.lang.reflect.Method;

import org.swxjava.Call;
import org.swxjava.Remoter;
import org.swxjava.Reply;
import org.swxjava.util.Log;
import org.swxjava.util.LogFactory;

public class DefaultRemoter implements Remoter {

	private static final Log LOG = LogFactory.getLog(DefaultRemoter.class);
	
	/* (non-Javadoc)
	 * @see org.swxjava.Remoter#execute(org.swxjava.Call)
	 */
	public Reply execute(Call call) throws Exception {
		// service instance
		Object target = call.getServiceInstance();
		// service method
		Method method = call.getMethod();
		if(LOG.isDebugEnabled()) {
			LOG.debug("Invoking the service method ... " + target.getClass().getName() + "#" + method.getName());
		}
		// call service method
		Object result = method.invoke(target, call.getParameters());
		// return result
		return new Reply(call.getCallId(), result, call.getUrl(), call.isDebug());
	}

}
